<?php
class Favorite
{
	private $db;
	
	function __construct() {
		$this->db = godhouse::app()->db;
	}
	
	public function find($condition) {
		if($condition) 
			$condition = "WHERE $condition";
		return $this->db->fetch_first("SELECT * FROM god_favorite $condition");
	}
	
	public function findByPk($pk) {
		return $this->db->fetch_first("SELECT * FROM god_favorite WHERE favoriteId='$pk'");
	}
	
	public function findAll($condition='', $page=1, $ppp=GODHOUSE_PPP3, $order='') {
		if($condition) 
			$condition = "WHERE $condition";
		if($order) {
			$condition .= "ORDER BY $order";
		} else 
			$condition .= " ORDER BY favoriteId DESC";
			
		$start = ($page-1)*$ppp;
		$data = $this->db->fetch_all("SELECT * FROM god_favorite $condition LIMIT $start, $ppp");
		foreach($data as &$v) {
			if(empty($v['url'])) {
				$url = GODHOUSE_DOMAIN_WWW."article-view.htm?favoriteId={$v['favoriteId']}";
				$v['url'] = $url;
			}
		}
		return $data;
	}
	
	public function findAllByPk($pk) {
		$data = $this->db->fetch_all("SELECT * FROM god_favorite WHERE favoriteId='$pk'");
		foreach($data as &$v) {
			print_r($v);exit;
			if(empty($v['url'])) {
				$url = GODHOUSE_DOMAIN_WWW."article-view.htm?favoriteId={$v['favoriteId']}";
				$v['url'] = $url;
			}
		}
		return $data;
	}
	
	public function update($condition, $att=array()){
		return $this->db->update('god_favorite', $att, $condition);
	}
	
	public function updateByPk($pk, $att=array()) {
		$condition = "favoriteId='$pk'";
		return $this->db->update('god_favorite', $att, $condition);
	}
	
	public function insert($att=array()){
		return $this->db->insert('god_favorite', $att);
	}
	
	public function delete($condition) {
		$this->db->query("DELETE FROM god_favorite WHERE $condition");
	}
	
	public function deleteByPk($pk) {
		$condition = "favoriteId='$pk'";
		$this->db->query("DELETE FROM god_favorite WHERE $condition");
	}
	
	public function count($condition) {
		if($condition) 
			$condition = "WHERE $condition";
		return $this->db->result_first("SELECT count(*) FROM god_favorite $condition");
	}
}
?>